package org.perlipse.internal.builder;

import org.eclipse.core.runtime.CoreException;

import org.eclipse.dltk.compiler.problem.IProblemReporter;
import org.eclipse.dltk.compiler.problem.ProblemSeverity;
import org.eclipse.dltk.core.ISourceRange;
import org.eclipse.dltk.core.builder.IBuildContext;
import org.eclipse.dltk.core.builder.IBuildParticipant;
import org.eclipse.dltk.core.builder.ISourceLineTracker;
import org.eclipse.dltk.launching.IScriptProcessHandler.ScriptResult;
import org.eclipse.dltk.launching.InternalScriptExecutor;
import org.eclipse.dltk.utils.TextUtils;

import org.scriptkitty.perl.compiler.CompilerOutput;
import org.scriptkitty.perl.compiler.CompilerOutputParser;
import org.scriptkitty.perl.compiler.ErrorsAndWarnings;


public class PerlBuildParticipant implements IBuildParticipant
{
    //~ Instance fields

    private InternalScriptExecutor executor;
    private String incPath;

    private CompilerOutputParser parser;

    //~ Constructors

    PerlBuildParticipant(String incPath, InternalScriptExecutor executor)
    {
        this.incPath = incPath;
        this.executor = executor;

        this.parser = new CompilerOutputParser();
    }

    //~ Methods

    @Override public void build(IBuildContext context) throws CoreException
    {
        // String path = context.getFile().getLocation().toOSString();
        String[] args = new String[] { "-c", "-w", incPath };

        IProblemReporter reporter = context.getProblemReporter();
        ISourceLineTracker tracker = context.getLineTracker();

        ScriptResult result = executor.execute(args, context.getContentsAsCharArray());

        for (CompilerOutput output : parser.parse(result.stderrLines))
        {
            if (!output.isLocal() || output.isCompilationAborted())
            {
                continue;
            }

            ErrorsAndWarnings.ErrorOrWarning eom = output.getErrorOrWarning();

            ProblemSeverity severity = getSeverity(eom);
            ISourceRange range = TextUtils.trimWhitespace(context.getSourceContents(),
                tracker.getLineInformation(output.lineNo - 1));

            PerlBuildProblem problem = new PerlBuildProblem(output.message, severity, range, output.lineNo - 1);

            reporter.reportProblem(problem);
        }
    }

    private ProblemSeverity getSeverity(ErrorsAndWarnings.ErrorOrWarning eom)
    {
        return (eom.isWarning()) ? ProblemSeverity.WARNING : ProblemSeverity.ERROR;
    }

/*
  <extension point="org.eclipse.ui.ide.markerResolution">
    <markerResolutionGenerator
      class="org.perlipse.internal.validators.ui.compiler.PerlCompilerMarkerResolutionGenerator"
      markerType="org.perlipse.validators.perlCompilerMarker" />
  </extension>

  <extension point="org.eclipse.dltk.ui.correctionProcessor">
    <correctionProcessor
      class="org.perlipse.internal.validators.ui.compiler.PerlCompilerCorrectionProcessor"
      nature="org.perlipse.core.perlipseNature" />
  </extension>
        ErrorOrWarning eom = PerlErrorsAndWarnings.getInstance().getErrorOrWarning(message);
        IValidatorProblem.Type type = getProblemType(eom);

        IValidatorProblem problem = new ValidatorProblem(fileName, message, lineNo, type);
        problem.addAttribute(PerlValidatorConstants.MARKER_ATTR_ERROR_WARN_DESC,
            eom.getDescription());

        return problem;
    }

    private IValidatorProblem.Type getProblemType(ErrorOrWarning eom)
    {
        return (eom.isWarning()) ? IValidatorProblem.Type.WARN : IValidatorProblem.Type.ERROR;
    }
 */
}
